import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_presentation/custom_model.dart';
import 'package:flutter_presentation/flutter_presentation.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _flutterPresentationPlugin = FlutterPresentation();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion = await _flutterPresentationPlugin.getPlatformVersion() ??
          'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              ElevatedButton(
                onPressed: () {
                  _flutterPresentationPlugin.startSubScreen(
                    CustomModel(
                      payAmt: "69.00",
                      payable: "23.00",
                      needAmt: "9.00",
                      fullScreen: false,
                      products: [
                        Product("农夫山泉", "2.00", 3),
                        Product("恰恰原味瓜子", "13.00", 1),
                      ],
                      ads: [
                        Ad(
                            type: Ad.TYPE_IMAGE,
                            url:
                                "https://t7.baidu.com/it/u=2537132411,3912460994&fm=193&f=GIF"),
                        Ad(
                            type: Ad.TYPE_VIDEO,
                            url:
                                "https://f.youchuangzhilian.com/Lj3aXpt6plqzxA3Nd4FCB96N5pi1J5C1/80cbc71a263a23cb3b6102415a487e10.mp4"),
                        Ad(
                            type: Ad.TYPE_IMAGE,
                            url:
                                "https://t7.baidu.com/it/u=1280638388,4029210594&fm=193&f=GIF"),
                      ],
                    ),
                  );
                },
                child: const Text("打开副屏"),
              ),
              ElevatedButton(
                  onPressed: () {
                    _flutterPresentationPlugin.loadData(
                      CustomModel(
                        payAmt: "123.00",
                        payable: "98.00",
                        needAmt: "19.00",
                        fullScreen: false,
                        products: [
                          Product("农夫山泉", "2.00", 3),
                          Product("恰恰原味瓜子", "19.00", 1),
                        ],
                        ads: [
                          Ad(
                            type: Ad.TYPE_IMAGE,
                            url:
                                "https://t7.baidu.com/it/u=1821636093,3729533292&fm=193&f=GIF",
                          ),
                          Ad(
                            type: Ad.TYPE_IMAGE,
                            url:
                                "https://t7.baidu.com/it/u=848096684,3883475370&fm=193&f=GIF",
                          ),
                        ],
                      ),
                    );
                  },
                  child: const Text("更新数据"))
            ],
          ),
        ),
      ),
    );
  }
}
